<template>
  <div class="threeElements">
    <div class="element">
      <el-form
        :model="elementData"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="手机：" prop="mdn">
          <el-input
            v-model.trim="elementData.mdn"
            placeholder="请输入手机号"
            autocomplete="off"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
          <el-input
            v-model.trim="elementData.name"
            placeholder="请输入姓名"
            autocomplete="off"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="证件号：" prop="idNo">
          <el-input
            v-model.trim="elementData.idNo"
            placeholder="请输入证件号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >核验</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCheckMdnNameIdNo } from "@/api/elements";
import debounce from "lodash.debounce";
import {
  isValidPhoneNumber,
  isValidNameRegex,
  isIdCardRegex,
} from "@/utils/validate";
export default {
  data() {
    // 手机号验证
    var validateMdn = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else if (!isValidPhoneNumber(value)) {
        callback(new Error("请输入合法的手机号"));
      } else {
        callback();
      }
    };
    // 姓名验证
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入姓名"));
      } else if (!isValidNameRegex(value)) {
        callback(new Error("请输入合法的姓名"));
      } else {
        callback();
      }
    };
    // 身份证号验证
    var checkIdNo = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请输入证件号"));
      } else if (!isIdCardRegex(value)) {
        callback(new Error("请输入合法的证件号"));
      } else {
        callback();
      }
    };
    return {
      elementData: {
        mdn: "", //手机号
        name: "", //姓名
        idNo: "", //身份证号
      },
      rules: {
        mdn: [{ validator: validateMdn, trigger: "blur" }],
        name: [{ validator: validateName, trigger: "blur" }],
        idNo: [{ validator: checkIdNo, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm: debounce(function (ruleForm) {
      this.$refs[ruleForm].validate(async (valid) => {
        if (valid) {
          let res = await getCheckMdnNameIdNo(this.elementData);
          if (res.code === 200) {
            this.$alert(`检测时间：${res.time}<br>${res.message}`, "检测结果", {
              confirmButtonText: "确定",
              dangerouslyUseHTMLString: true,
            });
          }
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    }, 3000),
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.threeElements {
  .element {
    width: 500px;
    margin: 0 auto;
    margin-top: 50px;
  }
}
</style>
